#ifndef VORONOI_PARSER_H
#define VORONOI_PARSER_H

#include <set>
#include <utility>
#include <vector>
#include <cmath>

namespace voronoi
{
	typedef std::set<unsigned int> Face;

	class VoronoiParser
	{
		public:
			VoronoiParser(unsigned int * pixels, int width, int height);
			~VoronoiParser();
			void parse(std::set<Face> * faces);

		protected:
			inline unsigned int readPoint(int x, int y) const
			{
				if(x>=m_width)  x = m_width-1;
				if(x<0)  x = 0;
				if(y>=m_height) y = m_height-1;
				if(y<0)  y=0;

				return m_pixels[y*m_width + x];
			}

			inline bool readFlag(int x, int y) const
			{
				if((x>=m_width) ||(x<0) || (y>=m_height)||(y<0))
					return false;

				return m_flags[y*m_width + x];
			}
			inline void read8Neighbours(int x, int y, unsigned int* neighbors) const
			{
				neighbors[0] = readPoint(x+1, y+0);
				neighbors[1] = readPoint(x+1, y+1);
				neighbors[2] = readPoint(x+0, y+1);
				neighbors[3] = readPoint(x-1, y+1);
				neighbors[4] = readPoint(x-1, y+0);
				neighbors[5] = readPoint(x-1, y-1);
				neighbors[6] = readPoint(x+0, y-1);
				neighbors[7] = readPoint(x+1, y-1);
			}

			bool isPolyFrontier(int x, int y) const;
			Face VoronoiParser::getConnectedVertexPixels(int xpos, int ypos);

			bool * m_flags;
			unsigned int * m_pixels;
			int m_width;
			int m_height;

			class PixelPos
			{
				public:
					PixelPos(int xpos, int ypos):x(xpos), y(ypos){}
					int x;
					int y;
					bool operator<(const PixelPos &other) const
					{
						if(x<other.x)
							return true;
						if(x>other.x)
							return false;
						if(y<other.y)
							return true;
						return false;
					}
			};


	};
}

#endif
